[Type] Tensor 5: Document and test needs_grad on the factories#525
Closed
hughperkins wants to merge 12 commits intohp/tensor-stork-4from
Closed
[Type] Tensor 5: Document and test needs_grad on the factories#525hughperkins wants to merge 12 commits intohp/tensor-stork-4from
hughperkins wants to merge 12 commits intohp/tensor-stork-4from
Conversation
needs_grad already passes through the factory **kwargs (PRs 2-3); this PR makes that contract explicit with tests and a user-guide section. Adds: - 6 tests covering grad allocation, primal+grad kernel round-trip on field and ndarray scalar backends, and grad on tensor_vec/tensor_mat field. - "Gradients" section in the user guide showing the needs_grad= usage on both backends with a runnable example. Documented limitation: qd.Vector.ndarray and qd.Matrix.ndarray do not currently accept needs_grad — that's an upstream Quadrants limitation and is noted in a code comment inside the test file. All tests pass; sphinx build clean.
adf0542 to
923d97e
Compare
2743267 to
e1540b7
Compare
923d97e to
a79a2d7
Compare
Drops the 'flexible' prefix from filenames and identifiers introduced in this branch series so the user-visible names are simply 'tensor'. Also strips PR-N back-references that will be meaningless once these PRs land. Touches only files owned by this series (no changes to external/ or unrelated tests).
- Apply black/ruff auto-fixes to _tensor.py and import ordering - Add pylint disable=import-outside-toplevel for late imports - Fix broken anchors #fields/#ndarrays in tensor.md (point to tensor_types.md) - Add Backend, tensor*, tensor_annotation to test_api.py expected names Made-with: Cursor
…r-stork-5 Made-with: Cursor # Conflicts: # docs/source/user_guide/tensor.md # python/quadrants/_tensor.py
The qd.tensor_vec / qd.tensor_mat factories already forward needs_grad through **kwargs (PR 3), but qd.Vector.ndarray and qd.Matrix.ndarray themselves did not accept the keyword. With Backend.NDARRAY in play, this meant qd.tensor_vec(..., backend=NDARRAY, needs_grad=True) raised TypeError, breaking the documented per-backend grad contract. Mirror the scalar qd.ndarray pattern (impl.ndarray:944-951): when needs_grad=True, allocate a companion grad ndarray of the same dtype and shape and wire it via _set_grad. Reject non-real element types with a clear QuadrantsRuntimeError. Silently ignore on the python backend, matching scalar ndarray. Tests cover allocation + shape parity, kernel-side primal/grad roundtrip, and the int-dtype rejection path, on both Vector and Matrix variants and through the qd.tensor_vec / qd.tensor_mat dispatchers.
…nsor Follow-up to the PR-3 privatization of tensor_vec / tensor_mat. The Vector/Matrix needs_grad tests added in this branch were calling the now-private qd.tensor_vec / qd.tensor_mat. Update them to the public qd.Vector.tensor / qd.Matrix.tensor classmethod surface. No behavioural change — same factories under the hood (Vector.tensor / Matrix.tensor delegate to _tensor_vec / _tensor_mat).
Replace the sister-test pairs in test_tensor_grad.py with
@pytest.mark.parametrize("backend", [FIELD, NDARRAY]) on a single test
function. Coverage widens for the FIELD side: we now run the
Vector/Matrix grad kernel roundtrips on FIELD as well as NDARRAY (only
NDARRAY had kernel-roundtrip coverage before; FIELD only had grad
allocation asserts).
Tests kept single-backend:
- test_tensor_vec_ndarray_needs_grad_rejects_int_dtype
- test_tensor_mat_ndarray_needs_grad_rejects_int_dtype
These probe the QuadrantsRuntimeError raised by the new
Vector.ndarray / Matrix.ndarray needs_grad gate. The FIELD path goes
through legacy create_field machinery with a different error class
and message; pinning that here would couple this branch's contract
to upstream behaviour outside our control.
Matrix.tensor and Vector.tensor classmethods were added in this PR but not reflected in the expected API snapshot, causing test_api[Matrix] to fail with an extra 'tensor' attribute. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
needs_grad already passes through the factory **kwargs (PRs 2-3); this PR makes that contract explicit with tests and a user-guide section.
Adds:
Documented limitation: qd.Vector.ndarray and qd.Matrix.ndarray do not currently accept needs_grad — that's an upstream Quadrants limitation and is noted in a code comment inside the test file.
All tests pass; sphinx build clean.
Issue: #
Brief Summary
copilot:summary
Walkthrough
copilot:walkthrough